package model

import (
	"time"

	"gorm.io/gorm"
)

// User 用户模型
type User struct {
	ID        uint           `gorm:"primaryKey" json:"id"`
	Username  string         `gorm:"type:varchar(50);uniqueIndex;not null;comment:用户名" json:"username"`
	Password  string         `gorm:"type:varchar(255);not null;comment:密码" json:"-"`
	Nickname  string         `gorm:"type:varchar(50);comment:昵称" json:"nickname"`
	Email     string         `gorm:"type:varchar(100);uniqueIndex;comment:邮箱" json:"email"`
	Phone     string         `gorm:"type:varchar(20);uniqueIndex;comment:手机号" json:"phone"`
	Avatar    string         `gorm:"type:varchar(500);comment:头像" json:"avatar"`
	Gender    int8           `gorm:"type:tinyint;default:0;comment:性别 0-未知 1-男 2-女" json:"gender"`
	Status    int8           `gorm:"type:tinyint;default:1;comment:状态 0-禁用 1-启用" json:"status"`
	Remark    string         `gorm:"type:varchar(500);comment:备注" json:"remark"`
	OrgID     uint           `gorm:"type:int;default:0;comment:所属组织ID" json:"orgId"`
	CreatedAt time.Time      `json:"createdAt"`
	UpdatedAt time.Time      `json:"updatedAt"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`

	// 关联
	Roles        []*Role       `gorm:"many2many:sys_user_role;" json:"roles,omitempty"`
	Organization *Organization `gorm:"foreignKey:OrgID" json:"organization,omitempty"`
}

// TableName 指定表名
func (User) TableName() string {
	return "sys_user"
}

// UserRole 用户角色关联表
type UserRole struct {
	UserID uint `gorm:"primaryKey"`
	RoleID uint `gorm:"primaryKey"`
}

// TableName 指定表名
func (UserRole) TableName() string {
	return "sys_user_role"
}
